package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.BizDuty;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.service.IBizDutyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 排班设置Controller
 * 
 * @author fmi110
 * @date 2021-07-25
 */
@Controller
@RequestMapping("/system/duty")
public class BizDutyController extends BaseController
{
    private String prefix = "system/duty";

    @Autowired
    private IBizDutyService bizDutyService;

    @RequiresPermissions("system:duty:view")
    @GetMapping()
    public String duty(ModelMap mmap)
    {
        SysDept dept = ShiroUtils.getSysUser().getDept();
        mmap.put("dept",dept );
        return prefix + "/duty";
    }

    @GetMapping("/select")
    public String select(String id,String day,String userName,ModelMap mmap)
    {
        BizDuty bizDuty = bizDutyService.selectBizDutyById(Long.valueOf(id));
        mmap.put("bizDuty", bizDuty);
        mmap.put("day",day );
        mmap.put("userName",userName );
        return prefix + "/select";
    }

    /**
     * 查询排班列表
     */
    @RequiresPermissions("system:duty:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizDuty bizDuty)
    {
        startPage();
        List<BizDuty> list = bizDutyService.selectBizDutyList(bizDuty);
        return getDataTable(list);
    }

    /**
     * 导出排班列表
     */
    @RequiresPermissions("system:duty:export")
    @Log(title = "排班设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizDuty bizDuty)
    {
        List<BizDuty> list = bizDutyService.selectBizDutyList(bizDuty);
        ExcelUtil<BizDuty> util = new ExcelUtil<BizDuty>(BizDuty.class);
        return util.exportExcel(list, "duty");
    }

    /**
     * 新增排班
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存排班
     */
    @RequiresPermissions("system:duty:add")
    @Log(title = "排班设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizDuty bizDuty)
    {
        return toAjax(bizDutyService.insertBizDuty(bizDuty));
    }

    /**
     * 修改排班
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BizDuty bizDuty = bizDutyService.selectBizDutyById(id);
        mmap.put("bizDuty", bizDuty);
        return prefix + "/edit";
    }

    /**
     * 修改保存排班
     */
    @RequiresPermissions("system:duty:edit")
    @Log(title = "排班设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizDuty bizDuty)
    {
        return toAjax(bizDutyService.updateBizDuty(bizDuty));
    }

    /**
     * 删除排班
     */
    @RequiresPermissions("system:duty:remove")
    @Log(title = "排班设置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizDutyService.deleteBizDutyByIds(ids));
    }

    /**
     * 删除排班
     */
    @RequiresPermissions("system:duty:edit")
    @Log(title = "排班设置", businessType = BusinessType.UPDATE)
    @PostMapping( "/updateByRow")
    @ResponseBody
    public AjaxResult updateBizDutyByIds(String ids,String dutyValue)
    {
        return toAjax(bizDutyService.updateBizDutyByIds(ids,dutyValue));
    }

    @RequiresPermissions("system:duty:add")
    @Log(title = "排班设置", businessType = BusinessType.INSERT)
    @PostMapping( "/setMonthDutyBatch")
    @ResponseBody
    public AjaxResult setMonthDutyBatch(String month)
    {
        return toAjax(bizDutyService.setMonthDutyBatch(month.split("-")[0], month.split("-")[1]));
    }
}
